/*
 * Copyright (C) 2004, Velocityme Partnership
 * StateActions.java
 *
 * Created on 09 October 2004, 02:41
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.www.actionforms.nodeactionforms.StateValueActionForm;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class StateActions extends NodeActions {
    
    /** Creates a new instance of StateActions */
    public StateActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatePK statePK = new StatePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("stateValue", cs.getStateValue(key, statePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatePK statePK = new StatePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        StateValueActionForm stateValueActionForm = new StateValueActionForm();
        session.setAttribute("stateActionForm", stateValueActionForm);
        StateValue stateValue = new StateValue();
        stateValue.setNodeValue(new NodeValue());
        session.setAttribute("stateValue", stateValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatePK statePK = new StatePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        StateValue stateValue = cs.getStateValue(key, statePK);
        session.setAttribute("stateValue", stateValue);
        StateValueActionForm stateValueActionForm = new StateValueActionForm();
        stateValueActionForm.setName(stateValue.getNodeValue().getName());
        stateValueActionForm.setDescription(stateValue.getNodeValue().getDescription());
        stateValueActionForm.setIsActive(stateValue.getIsActive());
        session.setAttribute("stateActionForm", stateValueActionForm);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            StateValue stateValue = (StateValue)session.getAttribute("stateValue");
            stateValue.getNodeValue().setName(((StateValueActionForm)form).getName());
            stateValue.getNodeValue().setDescription(((StateValueActionForm)form).getDescription());
            stateValue.setIsActive(((StateValueActionForm)form).getIsActive());
            StatePK statePK;
            if (stateValue.stateIdHasBeenSet()) {
                statePK = stateValue.getPrimaryKey();
                cs.stateEdit(key, statePK, stateValue);
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                statePK = cs.stateCreate(key, parentNodePK, stateValue);
            }
            nodePK = new NodePK(statePK.getStateId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
